﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;
using System.IO;

namespace FilStartUps.Account
{
    public partial class MemberProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (User.Identity.IsAuthenticated)
                {
                    LinkButton lkHome = (LinkButton)FilStartUps_PageNav1.FindControl("lbLgout");
                    LinkButton lkProfile = (LinkButton)FilStartUps_PageNav1.FindControl("lbProfile");
                    LinkButton lklogin = (LinkButton)FilStartUps_PageNav1.FindControl("lblogin");
                    LinkButton lkregister = (LinkButton)FilStartUps_PageNav1.FindControl("lbRegister");
                    lklogin.Visible = false;
                    lkregister.Visible = false;
                    lkProfile.Visible = true;
                    lkHome.Visible = true;
                }
                CheckRole();
                GetUserProfile();
            }

        }

        private void CheckRole()
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var member = (from m in db_connect.Members
                              where m.emailaddress.Equals(User.Identity.Name)
                              select m).SingleOrDefault();

                //if (member.roleidFK == 2)
                //{
                //    lbsetting.Visible = true;
                //    lbsetting.Enabled = true;
                //}
                //else
                //{
                //    lbsetting.Visible = false;
                //    lbsetting.Enabled = false;
                //}
            }
        }



        private void GetUserProfile()
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                //Prod
                //var profile = where(p=>p.username.equals(User.Name))
                //Test
                var member = db_connect.Members.Where(m => m.emailaddress.Equals(User.Identity.Name)).SingleOrDefault();

                var profile = db_connect.Profiles
                                    .Where(p => p.memberidFK.Equals(member.memberidPK))
                                        .SingleOrDefault();

                imgprofile.ImageUrl = "../FilStartUps.ashx?id=" + profile.profileidPK + "&cat=" + 0;
                lbprofileid.Text = profile.profileidPK.ToString();
                lblname.Text = profile.lastname;
                lbfname.Text = profile.firstname;
                lbphone.Text = profile.phonenumber;
                lbbirthdate.Text = profile.birthdate.ToShortDateString().ToString();
                lbcitizenship.Text = profile.citizenship;
                lbgender.Text = profile.gender;
                ltani.Text = profile.activitiesAndinterest;

                lbcompanyname.Text = profile.companyname;
                lbcompanywebsite.Text = profile.companywebsite;
                lbcompanywebsite.NavigateUrl = profile.companywebsite;
                lbcompanydescription.Text = profile.companydescription;
                lbcompanyaddress.Text = profile.companydescription;
            }
        }

        protected void lbeditprofile_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Account/EditProfile.aspx?id=" + lbprofileid.Text);
        }

    }
}